{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "内容相同的文件：\n",
      "D:\\高心阳\\python\\成绩文件汇总\\240052 窦鸣宇.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\240179陈金妙.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\240338张聪.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\240430党梦轩.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\240432王舒心.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\240433徐博恩.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\240778寇云霄.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\241021张佳堙.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\241041肖百禾.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\241060王佳怡.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\241181孟敬媛.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\241385张春浩.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\241444杨涵馥.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\241563张美辰.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\241632王杨宇飞.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\241730何晨雨.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\243902郑昱栋.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\243906沈冰悦.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\243907高心阳.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\243909唐岩.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\243911张延兴.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\243912葛李彤.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\243913付奕雅.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\243914许克.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\244501贺立上.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\244503宋瑞宇.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\244695陈乐轩.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\244696陈淑婷.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\244835 杨晨.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\245049刘怡楠.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\245053司竹颜.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\245056 郑浩.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\新建文本文档.txt\n",
      "D:\\高心阳\\python\\成绩文件汇总\\赵玮伊.txt\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "def find_duplicate_files(directory):\n",
    "    # 创建一个字典来存储文件内容及其对应的文件名\n",
    "    file_contents = {}\n",
    "\n",
    "    # 遍历目录中的所有文件\n",
    "    for root, dirs, files in os.walk(directory):\n",
    "        for file in files:\n",
    "            file_path = os.path.join(root, file)\n",
    "            # 读取文件内容\n",
    "            with open(file_path, 'r', encoding='utf-8') as f:\n",
    "                content = f.read()\n",
    "                # 如果内容已经存在于字典中，则添加文件名\n",
    "                if content in file_contents:\n",
    "                    file_contents[content].append(file_path)\n",
    "                else:\n",
    "                    file_contents[content] = [file_path]\n",
    "\n",
    "    # 找出内容相同的文件\n",
    "    duplicate_files = {content: files for content, files in file_contents.items() if len(files) > 1}\n",
    "\n",
    "    return duplicate_files\n",
    "\n",
    "# 指定目录\n",
    "directory = r\"D:\\高心阳\\python\\成绩文件汇总\"\n",
    "\n",
    "# 找出内容相同的文件\n",
    "duplicates = find_duplicate_files(directory)\n",
    "\n",
    "# 输出结果\n",
    "for content, files in duplicates.items():\n",
    "    print(\"内容相同的文件：\")\n",
    "    for file in files:\n",
    "        print(file)\n",
    "    print(\"\\n\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
